﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Configuration;
using System.Data.SqlClient;
using System.Web;
using MSL.DataBase.Core;

namespace MSL.DataBase.Lib
{
    /// <summary>
    /// 日志记录类
    /// </summary>
    internal class Log
    {
        /// <summary>
        /// 写日志
        /// </summary>
        /// <param name="message">信息</param>
        public static void WriteLog(string message)
        {
            if (DBConfig.IsWriteLog)
                WriteLogText(message);
        }
        /// <summary>
        /// 写日志
        /// </summary>
        /// <param name="fileName">文件名</param>
        /// <param name="message"></param>
        public static void WriteLog(string fileName, string message)
        {
            if (DBConfig.IsWriteLog)
            {
                WriteLogText(fileName, message);
            }
        }

        #region 私有方法

        /// <summary>
        /// 写日志到文件
        /// </summary>
        /// <param name="message">异常信息</param>
        private static void WriteLogText(string message)
        {
            if (!String.IsNullOrEmpty(DBConfig.LogPath))
            {
                try
                {
                    string folder = DBConfig.LogPath + @"\\db\\";
                    folder = folder.Replace(@"\\\\\", "\\");
                    if (!System.IO.Directory.Exists(folder))
                    {
                        System.IO.Directory.CreateDirectory(folder);
                    }
                    string todayKey = DateTime.Today.ToString("yyyyMMdd") + ".txt";
                    string filePath = folder + todayKey;
                    string pageUrl = System.Web.HttpContext.Current.Request.Url.ToString();
                    System.IO.File.AppendAllText(filePath, "\r\n------------------------\r\nlog:" + pageUrl + "\r\n" + message);
                }
                catch
                {
                    throw new Exception("Error on WriteLog :" + message);
                }
            }
        }
        /// <summary>
        /// 写日志到文件
        /// </summary>
        /// <param name="fileName">文件名，默认按时间命名，带上扩展名 如：20130104.txt</param>
        /// <param name="message"></param>
        private static void WriteLogText(string fileName, string message)
        {
            if (!String.IsNullOrEmpty(DBConfig.LogPath))
            {
                try
                {
                    string folder = DBConfig.LogPath + @"\\db\\";
                    folder = folder.Replace(@"\\\\\", "\\");
                    string todayKey = fileName;
                    if (!System.IO.Directory.Exists(folder))
                    {
                        System.IO.Directory.CreateDirectory(folder);
                    }
                    if (String.IsNullOrEmpty(fileName))
                    {
                        todayKey = DateTime.Today.ToString("yyyyMMdd") + ".txt";
                    }
                    string filePath = folder + todayKey;
                    string pageUrl = "";
                    if (System.Web.HttpContext.Current != null)
                    {
                        pageUrl = System.Web.HttpContext.Current.Request.Url.ToString();
                    }
                    System.IO.File.AppendAllText(filePath, "\r\n------------------------\r\nlog:" + pageUrl + "\r\n" + message);
                }
                catch
                {
                    //throw new Exception("Error on WriteLog :" + commandText);
                }
            }
        }
        #endregion
    }
}
